﻿using System;
using System.Collections.Generic;

namespace HurricanePackage
{
    /// <summary>
    /// 无参委托
    /// </summary>
    public class BroadcastAction : ICacheable
    {
        private List<Action> m_actionList;
        public List<Action> CallBackList => m_actionList;

        public int CallCount => m_actionList?.Count ?? 0;

        public Action this[int _index]
        {
            get
            {
                if (m_actionList != null && (_index >= 0 && _index < m_actionList.Count))
                    return m_actionList[_index];
                else
                    return null;
            }

        }

        public BroadcastAction(List<Action> _actionList)
        {
            if (_actionList != null)
                m_actionList = _actionList;
        }

        public void Init()
        {
            if (m_actionList == null)
                m_actionList = new List<Action>();
        }

        public void Reset()
        {
            m_actionList?.Clear();
        }

        public void Discard()
        {
            m_actionList?.Clear();
            m_actionList = null;
        }

        /// <summary>
        /// 添加委托,重复地址的则忽略
        /// </summary>
        /// <param name="_del"></param>
        public void AddCall(Action _del)
        {
            if (m_actionList == null)
                return;
            if (!m_actionList.Contains(_del))
                m_actionList.Add(_del);
        }
        /// <summary>
        /// 添加委托列表(覆盖)
        /// </summary>
        /// <param name="_call"></param>
        public void AddCall(BroadcastAction _call)
        {
            if (m_actionList == null || _call == null || _call.CallBackList == null)
                return;
            m_actionList.Clear();
            m_actionList.AddRange(_call.CallBackList);
        }
        /// <summary>
        /// 移除委托
        /// </summary>
        /// <param name="_del"></param>
        public void RemoveCall(Action _del)
        {
            m_actionList?.Remove(_del);
        }
    }
}